package com.hnkywl.sts.service.cwgl.kmys;

import com.hnkywl.sts.dao.cwgl.KmdmDao;
import com.hnkywl.sts.dao.cwgl.kmys.TyfzhsmcDao;
import com.hnkywl.sts.dao.cwgl.kmys.TyfzhsysDao;
import com.hnkywl.sts.entity.cwgl.Kmdm;
import com.hnkywl.sts.entity.cwgl.kmys.Tyfzhsmc;
import com.hnkywl.sts.entity.cwgl.kmys.Tyfzhsys;
import com.hnkywl.sts.entity.organ.Ck;
import com.hnkywl.sts.service.sys.YgService;
import com.hnkywl.sts.util.BaseUtils;
import com.hnkywl.sts.util.ReflectionUtils;
import net.ruixin.orm.hibernate.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-3-25
 * Time: 下午3:38
 * To change this template use File | Settings | File Templates.
 */
@Service
public class TyfzhsysService {
    private TyfzhsysDao tyfzhsysDao;
    private TyfzhsmcDao tyfzhsmcDao;
    private KmdmDao kmdmDao;

    /**
     * 查询通用辅助核算映射
     *
     * @param
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Transactional
    public String findAlltyfzhsys(Long tyfzhsmcId, String mc, String code, int pageNo, int pageSize) {
        Page<Tyfzhsys> kmdmPage = this.tyfzhsysDao.findAllTyfzhsys(tyfzhsmcId, mc, code, pageNo, pageSize);
        List<Tyfzhsys> kmdmList = kmdmPage.getResult() == null ? new ArrayList<Tyfzhsys>() : kmdmPage.getResult();
        if (kmdmList.size() > 0) {
            String nodes = "{\"total\":" + kmdmList.size() + ",\"rows\":[";
            StringBuffer json = new StringBuffer();
            if (kmdmList != null && !kmdmList.isEmpty()) {
                for (int i = 0; i < kmdmList.size(); i++) {
                    Tyfzhsys k = kmdmList.get(i);
                    json.append("{\"id\":\"");
                    json.append(k.getId());
                    json.append("\",\"mc\":\"");
                    json.append(k.getMc());
                    json.append("\",\"code\":\"");
                    json.append(k.getCode());
                    json.append("\",\"zjm\":\"");
                    json.append(k.getZjm()==null?" ":k.getZjm());
                    if (k.getSjtyfzhsys() != null) {
                        json.append("\",\"_parentId\":\"");
                        json.append(k.getSjtyfzhsys().getId());
                    } else {
                        json.append("\",\"iconCls\":\"");
                        json.append("icon-ok");
                        json.append("\",\"state\":\"");
                        json.append("closed");
                    }
                    json.append("\"},");
                }
                nodes += json.substring(0, json.length() - 1);
            }
            nodes += "]}";
            return nodes;
        } else {
            return "{\"total\":" + 0 + ",\"rows\":[]}";
        }
    }

    /**
     * @param id   辅助核算名称ID
     * @param flag flag=true combotree的ID为ID flag=false combotree的ID为code
     * @return
     */
    public String findAllTyfzhsysTree(Long id, boolean flag) {
        if (id != -1) {
            List<Tyfzhsys> list = tyfzhsysDao.findAllRootByMcId(id);
            Map<String, String> treeParam = new HashMap<String, String>();
            if (flag)
                treeParam.put("id", "id");
            else
                treeParam.put("id", "code");
            treeParam.put("text", "mc");
            treeParam.put("children", "tyfzhsysList");
            return BaseUtils.getTreeJson(list, treeParam);
        } else {
            List<Tyfzhsmc> list = tyfzhsmcDao.findAll();
            Map<String, String> treeParam = new HashMap<String, String>();
            if (flag)
                treeParam.put("id", "id");
            else
                treeParam.put("id", "code");
            treeParam.put("text", "mc");
            treeParam.put("attributes", "dl");
            treeParam.put("children", "tyfzhsysList");
            return BaseUtils.getTreeJson(list, treeParam);
        }
    }

    public Tyfzhsys findtyfzhsysDetail(Long id) {
        return this.tyfzhsysDao.get(id);
    }

    @Transactional
    public void updatetyfzhsys(Tyfzhsys tyfzhsys) {
        Tyfzhsys clone = this.tyfzhsysDao.get(tyfzhsys.getId());
        clone = (Tyfzhsys) ReflectionUtils.getInstance(clone, tyfzhsys);
        clone.setDl(tyfzhsmcDao.get(tyfzhsys.getTyfzhsmc().getId()).getDl());
        clone.setLastUpdated(new Date());
        if (clone.getSjtyfzhsys() != null && clone.getSjtyfzhsys().getId() == null) {
            clone.setSjtyfzhsys(null);
        }
        if (clone.getTyfzhsmc() != null && clone.getTyfzhsmc().getId() == null) {
            clone.setTyfzhsmc(null);
        }
        this.tyfzhsysDao.save(clone);
    }

    @Transactional
    public void addtyfzhsys(Tyfzhsys tyfzhsys) {
        if (tyfzhsys.getSjtyfzhsys() != null && tyfzhsys.getSjtyfzhsys().getId() == null) {
            tyfzhsys.setSjtyfzhsys(null);
        }
        if (tyfzhsys.getTyfzhsmc() != null && tyfzhsys.getTyfzhsmc().getId() == null) {
            tyfzhsys.setTyfzhsmc(null);
        }
        if (tyfzhsys.getZjm() == null || "".equals(tyfzhsys.getZjm())) {
            tyfzhsys.setZjm(" ");
        }
        if (tyfzhsys.getMc() == null || "".equals(tyfzhsys.getMc())) {
            throw new RuntimeException("映射名称不可为空");
        }
        if (tyfzhsys.getCode() == null || "".equals(tyfzhsys.getCode())) {
            throw new RuntimeException("映射代码不可为空");
        }
        tyfzhsys.setCreateUser(YgService.getCurrentYg());
        tyfzhsys.setCreateOrgan(YgService.getCurrentYg().getZggs());
        tyfzhsys.setCreateDepartment(YgService.getCurrentYg().getZgjg());
        tyfzhsys.setDateCreated(new Date());
        tyfzhsys.setLastUpdated(tyfzhsys.getDateCreated());
        tyfzhsys.setDl(tyfzhsmcDao.get(tyfzhsys.getTyfzhsmc().getId()).getDl());
        this.tyfzhsysDao.save(tyfzhsys);
    }

    @Transactional
    public void szTyfzhsys(Long tyfzhsysId,String szIds) {
        Tyfzhsys ys = tyfzhsysDao.get(tyfzhsysId);
       String[] ids = szIds.split(",");
        for(String id :ids){
            Kmdm k = kmdmDao.get(Long.parseLong(id));
            if(ys.getTyfzhsmc().getDl().equals("xjllxm")){
                k.setXjllsx("Y");
                k.setTyfzhsys(null);
                k.setTyfzhsmc(null);
                kmdmDao.save(k);
            }else{
                k.setXjllsx(null);
                k.setTyfzhsys(ys);
                k.setTyfzhsmc(ys.getTyfzhsmc());
                kmdmDao.save(k);
            }
        }
    }


    @Transactional
    public void deleteTyfzhsys(Long id) {
        Tyfzhsys tyfzhsys = this.tyfzhsysDao.get(id);
        if (!tyfzhsys.getTyfzhsysList().isEmpty()) {
            throw new RuntimeException("此通用辅助核算映射存在下级，不可删除！");
        }
        tyfzhsys.setLastUpdated(new Date());
        tyfzhsys.setDeleted(1);
        this.tyfzhsysDao.save(tyfzhsys);
    }


    @Autowired
    public void setTyfzhsysDao(TyfzhsysDao tyfzhsysDao) {
        this.tyfzhsysDao = tyfzhsysDao;
    }

    @Autowired
    public void setTyfzhsmcDao(TyfzhsmcDao tyfzhsmcDao) {
        this.tyfzhsmcDao = tyfzhsmcDao;
    }

    @Autowired
    public void setKmdmDao(KmdmDao kmdmDao) {
        this.kmdmDao = kmdmDao;
    }
}
